package com.ch3cooh.registry.util;

import com.ch3cooh.rpc.annotation.HttpRpc;

import java.net.URL;

public class StringUtils {

    public static boolean  isBlack(String str){
        return org.springframework.util.StringUtils.isEmpty(str);
    }

    public static String newString(byte[] bytes, String encode){
        try {
            return new String(bytes, encode);
        }catch (Exception e){
            return null;
        }
    }

    /**
     * 如果字符串首字母为大写字母，则将其转换为小写字母
     * @param str
     * @return
     */
    public static String firstCharUpper(String str){
        if(str!=null&Character.isUpperCase(str.charAt(0))){ //根据需要判断是大写还是小写，下面可以做相应的修改
            char[] charArray = str.toCharArray();
            if(charArray!=null){
                charArray[0]+=32; //此处是小写转大写，如果是大写转小写则是+=32，上面需要判断一下
                return String.valueOf(charArray);
            }
        }
        return str;
    }

    public static String castString(Object str){
        return (String)str;
    }


    /**
     * "file:/home/whf/cn/fh" -> "/home/whf/cn/fh"
     * "jar:file:/home/whf/foo.jar!cn/fh" -> "/home/whf/foo.jar"
     */
    public static String getRootPath(URL url) {
        String fileUrl = url.getFile();
        int pos = fileUrl.indexOf('!');

        if (-1 == pos) {
            return fileUrl;
        }

        return fileUrl.substring(5, pos);
    }

    /**
     * "cn.fh.lightning" -> "cn/fh/lightning"
     * @param name
     * @return
     */
    public static String dotToSplash(String name) {
        return name.replaceAll("\\.", "/");
    }

    /**
     * "Apple.class" -> "Apple"
     */
    public static String trimExtension(String name) {
        int pos = name.indexOf('.');
        if (-1 != pos) {
            return name.substring(0, pos);
        }

        return name;
    }

    /**
     * /application/home -> /home
     * @param uri
     * @return
     */
    public static String trimURI(String uri) {
        String trimmed = uri.substring(1);
        int splashIndex = trimmed.indexOf('/');

        return trimmed.substring(splashIndex);
    }

}
